package com.jike.noobmoney.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.rxbus.RxBus;
import com.jike.noobmoney.rxbus.RxBusRoute;
import com.jike.noobmoney.rxbus.RxEvent;
import com.jike.noobmoney.util.Logger;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by kekex on 2019/3/12.
 * 微信支付的回调
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {


    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, ConstantValue.Wechat_Appid);
        api.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Logger.e("xuke", "onPayFinish, errCode = " + resp.errCode);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();
                RxBus.getInstance().post(new RxEvent(RxBusRoute.WEIXIN));
            } else if (resp.errCode == -1) {
                Toast.makeText(this, "支付异常,请重新支付或联系客服", Toast.LENGTH_SHORT).show();
            } else if (resp.errCode == -2) {
                Toast.makeText(this, "已取消支付", Toast.LENGTH_SHORT).show();
            }
        }
        finish();
    }
}
